// --------------------------------------------------------------------------------------------------------------------
// <copyright file="KinectIRObjectDetectorConfigurationTest.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Test.Robotics.Vision
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Robotics.PhysicalObjects;
    using Microsoft.Robotics.Vision.ObjectDetection;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Test.Robotics.Runtime;

    /// <summary>
    /// Test for loading detector configuration
    /// </summary>
    [TestClass]
    [DeploymentItem(@"Data\ObjectDetection\Configurations\tomatoCanDetector_v3.xml", @"Data\ObjectDetection\Configurations")]
    public class KinectIRObjectDetectorConfigurationTest
    {
        /// <summary>
        /// Loading the configuration file
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void ObjectDetectionCreateDetectorConfiguration()
        {
            string configFile = @"Data\ObjectDetection\Configurations\tomatoCanDetector_v3.xml";

            KinectIRObjectDetectorConfiguration config = KinectIRObjectDetectorConfiguration.Create(configFile);
            Assert.AreEqual(config.Level1Threshold, 0.47, 0.15, "Level 1 threshold");
            Assert.AreEqual(config.Level2Threshold, 0.91, 0.15, "Level 2 threshold");
            Assert.AreEqual(config.ConfidenceThreshold, 0.80, 0.15, "Confidence threshold");
            Assert.AreEqual(config.Level2Patch.Width, 30, "Level 2 Patch width");
            Assert.AreEqual(config.Level2Patch.Height, 44, "Level 2 PatchCluster height");
            Assert.AreEqual(config.NoReadingDepthValue, 2500, "No reading value");
            Assert.AreEqual(config.ObjectHeightMM, 110, "Object height");
            Assert.AreEqual(config.ObjectWidthMM, 75, "Object width");
            Assert.AreEqual(config.ObjectDepthMM, 75, "Object depth");
            Assert.AreEqual(config.PatchContextScale, 1.2, 0.1, "Patch scale");
            Assert.AreEqual(config.DepthNormalizationFactor, 1.5, 0.1, "Patch scale");

            KinectIRObjectDetectorConfiguration config1 = new KinectIRObjectDetectorConfiguration(
                                new PhysicalObjectDescription(PhysicalObjectShape.Cylinder, config.ObjectWidthMM / 1000.0, config.ObjectHeightMM / 1000.0, config.ObjectDepthMM / 1000.0, 200, "canTest"),
                                config.Level1Classifier,
                                config.Level2Classifier,
                                config.Level2Regress,
                                config.Level2OrientationRegress,
                                config.ConfidenceEstimator,
                                config.Level2Patch,
                                config.Level1Threshold,
                                config.Level2Threshold,
                                config.ConfidenceThreshold,
                                config.PatchContextScale,
                                config.DepthNormalizationFactor,
                                config.NoReadingDepthValue);

            Assert.AreEqual(config.Level1Threshold, config1.Level1Threshold);
            Assert.AreEqual(config.Level2Threshold, config1.Level2Threshold);
            Assert.AreEqual(config.ConfidenceThreshold, config1.ConfidenceThreshold);
            Assert.AreEqual(config.Level2Patch.Width, config1.Level2Patch.Width);
            Assert.AreEqual(config.Level2Patch.Height, config1.Level2Patch.Height);
            Assert.AreEqual(config.NoReadingDepthValue, config1.NoReadingDepthValue);
            Assert.AreEqual(config.PatchContextScale, config1.PatchContextScale);
            Assert.AreEqual(config.DepthNormalizationFactor, config1.DepthNormalizationFactor);
            Assert.AreEqual(config.ObjectHeightMM, config1.ObjectHeightMM);
            Assert.AreEqual(config.ObjectWidthMM, config1.ObjectWidthMM);
        }

        /// <summary>
        /// test configuration load failure
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        [ExpectedException(typeof(System.IO.FileNotFoundException))]
        public void ObjectDetectionCreateDetectorConfigurationFail()
        {
            string configFile = @"notExist.xml";
            KinectIRObjectDetectorConfiguration config = KinectIRObjectDetectorConfiguration.Create(configFile);
        }
    }
}
